<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Document extends MY_Luckydog
{
	public function __construct()
	{
		parent::__construct();
        
        $this->load->model('luckydog/Document_model', 'doc');
	}

/**
 * 新闻列表
 * @return [type] [description]
 */
    public function index(){
        $type = $this->input->get("type",true) ? $this->input->get("type",true) : 1;
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $_SESSION['v_menu'] = 'document';
        $filter = array("type"=>$type);
        $limit = 20;
        $total_rows = $this->doc->getDocCnt($filter,$per_page,$limit);
        $base_url = '/luckydog/document/index?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);

        $documentList = $this->doc->getDocList($filter,$per_page, $limit);
        $data = array(
            'documentList'=>$documentList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'total_rows'=>$total_rows,
            'type'=>$type
            );
        $this->load->view("/luckydog/document/index", $data);
    }

    public function textures()
    {
        $id = $this->input->get("id",true)?$this->input->get("id",true):"";
        $type = $this->input->get("type",true)?$this->input->get("type",true):0;
        
        $_SESSION['v_menu'] = 'document';
        $doc_cat_list = $this->doc->get_cat_list();
        $data = ['doc_cat_list'=>$doc_cat_list];
        if(!empty($id)){
            $docInfo = $this->doc->getDocInfobyID($id);
            $data["docInfo"] = $docInfo;
            $data['id'] = $id;
        }
        $data['type'] = $type;
    	$this->load->view("/luckydog/document/textures", $data);
    }



    //编辑新闻
    public function editdoc(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $cat_id = $this->input->post("cat_id",true) ? $this->input->post("cat_id",true) : 1;
        $content = $this->input->post("contents") ? $this->input->post("contents") : "";
        $title = $this->input->post("title",true) ? $this->input->post("title",true) : "";
        
        $data = array(
            "content"=>$content ,
            "title"=>$title ,
            "cat_id"=>$cat_id,
            "operationname"=>$_SESSION['adminUserInfo']['id'],
        );
        if(empty($id)){
            $data['addtime']=time();
        }
        $res = $this->doc->editDoc($id,$data);
        echo $res;
    }


    //编辑上线下，删除状态
    public function deldoc(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $res =$this->doc->delDoc($id);
        echo $res;
    }

     //上传文件
    public function uploadMessage()
    {

        $documentid = $this->input->post("documentid",true) ? $this->input->post("documentid",true) : 0;
        $title = $this->input->post("title",true) ? $this->input->post("title",true) : 0;
        $departmentid = $this->input->post("departmentid",true) ? $this->input->post("departmentid",true) : 0;
        $fileFormat = ['txt','doc','pdf','xlsx','gif','jpg','jpeg','bmp','png','xls','ppt','docx','pptx'];
        if(!empty($documentid)){
            //执行编辑
            $data = array(
                'id'=>$departmentid,
                'title'=>$title,
                'operationname'=>$_SESSION['adminUserInfo']['username'],
            );
            $res = $this->doc->editDoc($documentid, $data);
            if($res){
                header("Content-type:text/html;charset=utf-8");
                echo "<script>alert('添加成功');window.location.href='/luckydog/document/index';</script>";
                exit();
            }else{
                header("Content-type:text/html;charset=utf-8");
                echo "<script>alert('添加失败');window.location.reload();</script>";
                exit();
            }

        }else{
            if (empty($_FILES)) {
                //$this->error('必须选择上传文件');
            } else {
               $this->load->library('UploadFile');
                $upload = new UploadFile();// 实例化上传类
                $upload->maxSize = 5242880;// 5M设置附件上传大小
                $upload->allowExts = $fileFormat;// 设置附件上传类型
                //$upload->saveRule = $this->fileNameSaveRule();
                $insert_save_path = '/document/'.date('Y-m-d').'/';
                $savepath = './uploads/document/'.date('Y-m-d').'/';
                if (!file_exists($savepath)) {
                    mkdir($savepath);
                }
                $upload->savePath = $savepath;// 设置附件上传目录
                $upload->uploadReplace = true;
                $uploadStauts = $upload->upload();
                if (!$uploadStauts) {// 上传错误提示错误信息
                    header("Content-type:text/html;charset=utf-8");
                    echo "<script>alert('".$upload->error($uploadStauts)."');window.location.reload();</script>";
                    exit();
                } else {// 上传成功 获取上传文件信息
                    $info = $upload->getUploadFileInfo();
                    $insertStr = "";
                    $insertData_arr = [];
                    foreach ($info as $key => $value) {
                        $insertData_arr[] = "('".$title."','".$_SESSION['adminUserInfo']['username']."','".$insert_save_path.$value['savename']."',".time().",".$departmentid.",'".$value['name']."'".")";
                    }
                    $insertStr = implode(",",$insertData_arr);
                    $res = $this->doc->editDoc("",$insertStr);
                    if($res){
                        header("Content-type:text/html;charset=utf-8");
                        echo "<script>alert('添加成功');window.location.href='/luckydog/document/index';</script>";
                        exit();
                    }else{
                        header("Content-type:text/html;charset=utf-8");
                        echo "<script>alert('添加失败');window.location.href='/luckydog/document/index';</script>";
                        exit();
                    }


                }
            }
        }

    }

    public function fileNameSaveRule(){
        return generate_ordersn();
    }


    //编辑上线下，删除状态
    public function editStatus(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $data = array(
            "id"=>$id,
        );
        $res =$this->doc->editStatus($data);
        echo $res;
    }


}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/index.php */
